home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-06-18 | 2.5 KB | 72 lines | [TEXT/MPS ] |
- // A TMON Professional Prime Number Generator
- // By Jay Zipnick 6/18/91
- //
- // This is a TMON Professional script which generates prime numbers
- // This implementation divides prime candidates by the known primes
- // up to the square root of the candidate. If every division of the
- // candidate has a remainder the candidate is prime. To save the
- // "known prime" divisors this uses the 512 bytes at PlayMem, and
- // as a result this implementation is limited to testing integers
- // below the square of the 257th prime. That is it can generate prime
- // number up to 2,627,641.
- //
- // Changing the variable €LoggingOutput to a non empty file name
- // (e.g., "Primes.Out") will save output to a file with this name.
- // Note that the implementation below uses the printf dcmd when
- // logging the output to a disk file.
- //
- ////////////////////////////////////////////////////////////////////////////////
-
-
- //////// Output control
-
- Vars €LoggingOutput = "" // NIL or the name of the output file
-
- If €LoggingOutput == "" // Define output function: screen only or to disk
- Alias Output,"Open Number Ω" // Display in Num window (screen only)
- Else
- Printing /File = €LoggingOutput // Set the output file
- Alias Output," ∂
- Print printf ∂"%d∂" Ω ∂
- Open Number Ω" // Print to disk and show in Num window
- End
-
-
- //////// Array storage
-
- Vars €primesArray = PlayMem // Remember small primes in an array
- Vars €primesEnd = PlayMem+.512 // And define alias for setting array entry
- Vars €primesPtr = €primesArray
- Alias storePrime," ∂
- If €primesPtr < €primesEnd ∂
- Fill €primesPtr..€primesPtr+1,Ω<<.16 ∂
- Vars €primesPtr = €primesPtr+2 ∂
- End"
-
-
- //////// Initial primes (non calculated)
-
- Output 2 // We do not calculate the first two primes
- Output 3
- storePrime 3 // Save this in our prime divisor array
-
-
- //////// Prime number generator
-
- Vars €test=5 // Start testing for primes from here on
- While 1 // Go “forever”, or until command-period
- Vars €testPtr = €primesArray // Point to list of prime divisors
- Vars €testDiv = €testPtr^.W // Get first one
- While (€testDiv*€testDiv <= €test) // Divide by primes up to SquareRoot of €test
- break if !(€test \\ €testDiv) // Break if €testDiv is an even divisor
- Vars €testPtr = €testPtr + 2 // Get next test divisor
- Vars €testDiv = €testPtr^.W
- End
-
- If €testDiv*€testDiv > €test // Was €test a prime number?
- Output €test // If so, output it
- StorePrime €test // Remember it
- End
- Vars €test = €test + 2 // Get next candidate to check (skip evens)
- End
-